package testing;

import java.util.List;
import org.junit.Test;
import common.messages.KVAdminMessage;
import common.messages.KVMessage;
import common.messages.MetaData;
import communicationLogic.ClientSocket;
import app_kvClient.KVClient;
import app_kvEcs.ECS;
import client.KVStore;
import junit.framework.TestCase;

public class AdditionalTest extends TestCase {

    // TODO add your test cases, at least 5

    private KVClient client;
    private KVStore kvClient;
    private ClientSocket cs;
    Exception ex = null;
    KVMessage response = null;

    ECS ecs = new ECS("ECS.config");
    KVAdminMessage kvadmin = null;
    String ip = "";
    int port = 0;
    String start = "";
    String end = "";

    public void setUp() {
        ecs.initService(1);
        ecs.start();

        List<MetaData> metaDatas = KVAdminMessage.sortMetaData(ecs.activeMap);
        for (MetaData m : metaDatas) {
            ip = m.getIp();
            port = Integer.parseInt(m.getPort());
            start = m.getHead();
            end = m.getTail();
        }

        kvClient = new KVStore(ip, port);
        try {
            kvClient.connect();
        } catch (Exception e) {
        }
    }

    public void tearDown() {
        kvClient.disconnect();
        ecs.stop();
        ecs.shutDown();
    }

    @Test
    public void testSetLogLevel() {
        client = new KVClient();
        try {
            client.setLevel("asdf");
        } catch (Exception e) {
            ex = e;
        }
        assertNull(ex);
    }

    @Test
    public void testPutLongkey() {
        String key = "foooooooooooooooooooooooooooooo";
        String value = "bar";
        try {
            response = kvClient.put(key, value);
        } catch (Exception e) {
            ex = e;
        }
        assertTrue(ex == null && response != null);
    }

    @Test
    public void testReDisconnect() {
        try {
            cs = new ClientSocket(ip, port);
            cs.connect();
            cs.disConnect();
            cs.disConnect();
        } catch (Exception e) {
            ex = e;
        }
        assertNull(ex);
    }

    @Test
    public void testMetaData() {
        int initSize = 0;
        try {
            kvadmin = new KVAdminMessage();
            kvadmin.setMetaList(KVAdminMessage.sortMetaData(ecs.activeMap));
            initSize = kvadmin.getMetaList().size();

        } catch (Exception e) {
            ex = e;
        }
        assertTrue(ex == null && initSize == 1);
    }

    @Test
    public void testAddnode() {
        int initSize = 0;
        int addedSize = 0;
        try {
            kvadmin = new KVAdminMessage();
            kvadmin.setMetaList(KVAdminMessage.sortMetaData(ecs.activeMap));
            initSize = kvadmin.getMetaList().size();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            ecs.addNode();
            kvadmin.setMetaList(KVAdminMessage.sortMetaData(ecs.activeMap));
            addedSize = kvadmin.getMetaList().size();
        } catch (Exception e) {
            ex = e;
        }
        assertTrue(ex == null && addedSize == initSize+1);
    }

    @Test
    public void testRemoveNode() {
        int initSize = 0;
        int removedSize = 0;
        try {
            kvadmin = new KVAdminMessage();
            kvadmin.setMetaList(KVAdminMessage.sortMetaData(ecs.activeMap));
            initSize = kvadmin.getMetaList().size();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            ecs.addNode();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            ecs.removeNode();
            kvadmin.setMetaList(KVAdminMessage.sortMetaData(ecs.activeMap));
            removedSize = kvadmin.getMetaList().size();
            
        } catch (Exception e) {
            ex = e;
        }
        assertTrue(ex == null && initSize == removedSize);
    }

}
